package top.codedance.iotp.tools.serverTool.subframe;

import top.codedance.iotp.iot.sdk.entity.DeviceEntity;
import top.codedance.iotp.tools.owner.ToolCallOwner;
import top.codedance.iotp.tools.serverTool.ui.AddDeviceFrame;
import top.codedance.iotp.tools.serverTool.ui.GenDeviceFrame;
import top.codedance.iotp.tools.terminal.ui.UserVerificationCertificateManager;

import javax.swing.*;
import java.net.URL;

/**
 *
 * @author Lenovo
 */
public class AddDeviceSubFrame extends javax.swing.JDialog implements ToolCallOwner {
    private AddDeviceFrame addDeviceFrame;

    /**
     * Creates new form AddDeviceSubFrame
     */
    public AddDeviceSubFrame(AddDeviceFrame addDeviceFrame) {
        super(addDeviceFrame, true);
        this.addDeviceFrame = addDeviceFrame;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setLocationRelativeTo(this.addDeviceFrame.getMainFrame().getShellFrame());
        setTitle("代码飞舞采控系统-添加设备");
    }

    @Override
    public void setValue(String value, int type) {
        switch (type) {
            case 0:
                deviceIdTf.setText(value);
                break;
        }

    }

    @Override
    public String getValue(int type) {
        String result = "";
        switch (type) {
            case 0:
                result = deviceIdTf.getText();
                break;
        }
        return result;
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        deviceIdTf = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        unitNameTf = new javax.swing.JTextField();
        addBtn = new javax.swing.JButton();
        jb_genId = new javax.swing.JButton();
        jb_enOrDeCode = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("设备Id: ");

        jLabel2.setText("单位名称: ");

        addBtn.setLabel("添加");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });

        jb_genId.setText("生成ID...");
        jb_genId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_genIdActionPerformed(evt);
            }
        });

        jb_enOrDeCode.setText("加解密...");
        jb_enOrDeCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_enOrDeCodeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(unitNameTf)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(deviceIdTf)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_genId)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_enOrDeCode)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(279, 279, 279)
                .addComponent(addBtn)
                .addContainerGap(265, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(deviceIdTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_genId)
                    .addComponent(jb_enOrDeCode))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(unitNameTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(addBtn)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed
        if(!deviceIdTf.getText().equals("") && !unitNameTf.getText().equals("")) {
            DeviceEntity deviceEntity = new DeviceEntity();
            deviceEntity.setDeviceId(deviceIdTf.getText());
            deviceEntity.setUnitName(unitNameTf.getText());
            addDeviceFrame.addAction(deviceEntity);
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(rootPane, "设备号与单位名称都不能为空。");
        }
    }//GEN-LAST:event_addBtnActionPerformed

    private void jb_enOrDeCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_enOrDeCodeActionPerformed
        UserVerificationCertificateManager uvcm = new UserVerificationCertificateManager(addDeviceFrame.getMainFrame().getShellFrame(), this, 0);
        uvcm.setLocationRelativeTo(addDeviceFrame.getMainFrame().getShellFrame());
        uvcm.setVisible(true);
    }//GEN-LAST:event_jb_enOrDeCodeActionPerformed

    private void jb_genIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_genIdActionPerformed
        GenDeviceFrame gdf = new GenDeviceFrame(addDeviceFrame.getMainFrame(), this, 0);
        gdf.setVisible(true);
    }//GEN-LAST:event_jb_genIdActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JTextField deviceIdTf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jb_enOrDeCode;
    private javax.swing.JButton jb_genId;
    private javax.swing.JTextField unitNameTf;
    // End of variables declaration//GEN-END:variables
}
